In [3]:
from candlesimport import CandlesImport

import pandas as pd
import matplotlib
import datetime
import holoviews as hv
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings("ignore")
from holoviews.operation.timeseries import rolling, rolling_outlier_std
from holoviews.streams import Stream
hv.notebook_extension('bokeh')
In [36]:
def to_dataFrame(filename):
    cInport = CandlesImport()
    candles = cInport.importFromPickle(filename)
    df = pd.DataFrame.from_dict(candles)
    df['date'] = pd.to_datetime(df['date']*10**9)
    df.set_index(['date'], inplace=True)
    return df
In [37]:
candles_btc, candles_ltc = map(to_dataFrame,('data/btc360.p','data/ltc360.p'))
In [38]:
candles_btc.tail()
Out[38]:
close high low open quoteVolume volume weightedAverage
date
2018-01-27 01:25:00 10940.192199 10940.192199 10909.301221 10915.178133 7.908582 86400.845385 10924.947907
2018-01-27 01:30:00 10985.561390 11000.000000 10925.218823 10940.192199 7.954362 87136.600451 10954.568611
2018-01-27 01:35:00 11016.895683 11016.895683 10966.057770 10985.561390 7.168100 78829.286364 10997.236473
2018-01-27 01:40:00 10998.072717 11030.052477 10988.505416 11016.895683 1.696027 18663.869323 11004.464482
2018-01-27 01:45:00 10985.072717 10990.523360 10953.460154 10990.523360 5.097566 55965.885753 10978.942520
In [39]:
candles_ltc.tail()
Out[39]:
close high low open quoteVolume volume weightedAverage
date
2018-01-27 01:25:00 174.000000 174.522096 174.000000 174.000001 22.175336 3858.517629 174.000410
2018-01-27 01:30:00 174.522096 174.522096 173.599999 174.000000 147.353450 25582.368039 173.612278
2018-01-27 01:35:00 175.215686 175.215686 174.028567 174.726000 4.222041 736.724025 174.494742
2018-01-27 01:40:00 175.000000 175.449184 174.522097 175.215686 18.370926 3217.186198 175.123786
2018-01-27 01:45:00 175.449182 175.449182 175.449182 175.449182 0.017376 3.048593 175.449182
In [40]:
def load_symbol(symbol, **kwargs):
    df = data[symbol]
    df['date'] = df.index
    return hv.Curve(df, ('date', 'Date'), ('close', 'Close price'))

stock_symbols = ['LTC', 'BTC',]
data = {"LTC": candles_ltc, 'BTC':candles_btc}
dmap = hv.DynamicMap(load_symbol, kdims='Symbol').redim.values(Symbol=stock_symbols)
In [41]:
%%opts Curve [width=700] {+framewise}
dmap
Out[41]:
In [42]:
%%opts Scatter [width=700] (color='black')
smoothed = dmap*rolling(dmap, rolling_window=30)*rolling_outlier_std(dmap)
smoothed
Out[42]:

Correlation

Looking correlation of Litecoin and Bitcoin

In [43]:
%%opts Curve [width=700, height=500, show_grid=True] {+framewise}
def rolling_corr(w):
    S = candles_ltc['close'].rolling(w).corr(other=candles_btc['close'])
    candles_ltc['corrBTC'] = S
    return hv.Curve(candles_ltc.iloc[-2000:], ('date', 'Date'), ('corrBTC','Rolling correlation'))
    
winsize = {s:rolling_corr(s) for s in range(10,60,2)}
rcorr = hv.HoloMap(winsize, kdims='Window size')
rcorr
Out[43]:

Volatility

Volatility is the variance of the price. If an assert is more volatility, it is more risky.

In [44]:
%%opts Curve [width=700, height=500, show_grid=True] {+framewise}
def rolling_vol(w, symbol):
    dt = data[symbol]
    S = dt['close'].rolling(w).std()
    dt['Volatility'] = S
    dt['date'] = candles_ltc.index

    return hv.Curve(dt.iloc[-2000:], ('date', 'Date'), ('Volatility','Rolling volatility'))

stock_symbols = ['LTC', 'BTC',]
data = {"LTC": candles_ltc, 'BTC': candles_btc}    
winsize = range(10,60,2)
rvol = hv.DynamicMap(rolling_vol, kdims=['Wsize', 'Symbol']).redim.values(Symbol=stock_symbols).redim.range(Wsize=(10,60))
rvol
Out[44]:

Return rate

In this graph, we will calculate the return rate, that is $$ R = \frac{P_f}{P_p} - 1 $$ where $P_f$ is the price future and $P_p$ is the price of the past

In [45]:
candles_ltc['5m_return'] = candles_ltc.close/candles_ltc.close.shift(1) - 1
candles_btc['5m_return'] = candles_btc.close/candles_btc.close.shift(1) - 1
In [47]:
%%opts Curve [width=700, height=500, show_grid=True] {+framewise}
hv.Curve(candles_ltc[-10000:], ('date', 'Date'),('5m_return', '5m Return rate'), label='Litecoin returns').hist(num_bins=150)
Out[47]: